import 'package:word_game/engine/core.dart';
import 'package:word_game/engine/game_event_bus.dart';
import 'battle_manager.dart';
import 'battleable.dart';
import 'kill_monster_event.dart';
import 'monster.dart';
import 'monster_cluster.dart';

class BattleEvent extends Eventable {
  BattleEvent() : super('战斗'){

  }
  @override
  void doEvent(dynamic owner) async {
    Monster sight = owner as Living;
    List<Monster> monsters = null;
    if (sight is MonsterCluster) {
      monsters = sight.members;
    } else {
      monsters = [sight];
    }
    Hero hero = World.instance.hero;
    Engine.instance.displayer.displayString(hero.name + '开始与' + sight.name + '战斗');
    bool result = await BattleManager.instance.battle(hero as Battleable, monsters);
    if (result) {
      Engine.instance.displayer.displayString('${hero.name}获胜。');
      hero.onVictory();
      for (var monster in monsters) {
        for (var item in monster.processes) {
          item.process(hero);
        }
        GameEventBus.instance.fire(KillMonsterEvent(hero, monster));
      }
      sight.onDefeated();
    } else {
      Engine.instance.displayer.displayString('${sight.name}获胜。');
      hero.onDefeated();
      for (var monster in monsters) {
        monster.onVictory();
      }
    }
  }
}